// pages/my/addAdress/addAdress.js
const utils = require("../../../utils/utils.js");
var wxMarkerData = [];
Page({
    data: {
        address: '',
        mobile: '',
        name: '',
        lng: '',
        lat: '',
        switch1Checked: false,
        id: ''
    },
    switch1Change(e) {
        this.setData({
            switch1Checked: e.detail.value
        })
    },
    btnConfirm() {
        var name = this.data.name;
        var mobile = this.data.mobile;
        var address = this.data.address;
        var houseNumber = this.data.houseNumber
        if (!utils.is_define(name)) {
            utils.toast("请输入收货人姓名");
            return false;
        }
        if (!utils.is_define(mobile)) {
            utils.toast("请输入手机号");
            return false;
        }
        if (!utils.is_mobile(mobile)) {
            utils.toast("请输入正确的手机号");
            return false;
        }
        if (!utils.is_define(address)) {
            utils.toast("请选择收货地址");
            return false;
        }
        if (!utils.is_define(houseNumber)) {
            utils.toast("请输入楼号和门牌号");
            return false;
        }
        var isDefault = -1
        if (this.data.switch1Checked) {
            isDefault = 1
        }
        if (utils.is_define(this.data.id)) {
            var data = {
                id:this.data.id,
                realName:name,
                tel:mobile,
                address:address,
                lat:this.data.lat,
                lng:this.data.lng,
                defaultAddress:isDefault,
                houseNumber:houseNumber
            }
            wx.showLoading({
              title: '',
              mask:true
            })
            utils.request('address/edit',data,function(res){
                wx.hideLoading()
                if(res.data.code==200){
                    utils.toast(res.data.msg)
                    setTimeout(function(){
                        wx.navigateBack()
                    },1500)
                }else{
                    utils.toast(res.data.msg)
                }
            })
        } else {
            var data = {
                realName: name,
                tel: mobile,
                address: address,
                lat: this.data.lat,
                lng: this.data.lng,
                defaultAddress: isDefault,
                houseNumber:houseNumber
            }
            wx.showLoading({
                title: '',
                mask: true
            })
            utils.request('address/add', data, function (res) {
                wx.hideLoading()
                if (res.data.code == 200) {
                    utils.toast(res.data.msg)
                    setTimeout(function () {
                        wx.navigateBack()
                    }, 1500)
                } else {
                    utils.toast(res.data.msg)
                }
            })
        }
    },
    inputhouseNumber(e) {
        this.setData({
            houseNumber: e.detail.value
        })
    },
    inputName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    inputMobile(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    chooseAddress() {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                var name = res.name
                var address = res.address
                var latitude = res.latitude
                var longitude = res.longitude
                that.setData({
                    address: address+name,
                    lng: longitude,
                    lat: latitude
                })

            },
            complete(r) {

            }
        })

    },
    onLoad: function (e) {
        if (utils.is_define(e.id)) {
            this.setData({
                id: e.id
            })
            this.getIndex()
        }
    },
    getIndex() {
        wx.showLoading()
        var that = this;
        utils.request('address/detail/' + that.data.id, {}, function (res) {
            wx.hideLoading()
            if (res.data.code == 200) {
                that.setData({
                    address: res.data.data.address,
                    mobile: res.data.data.tel,
                    name: res.data.data.realName,
                    lng: res.data.data.lng,
                    lat: res.data.data.lat,
                    houseNumber:res.data.data.houseNumber,
                    switch1Checked: res.data.data.defaultAddress == 1 ? true : false
                })
            } else {
                utils.toast(res.data.msg)
            }
        })
    }

})